package com.defysoft.waukids.notice;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.R;
import com.defysoft.waukids.gallery.CustomGallery;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.polites.android.GestureImageView;

public class Notice_Write_Photo_View extends FragmentActivity{
	Toolbar toolbar;
	ViewPager mPager;
	CustomGallery data;
	ArrayList<CustomGallery> dataT;
	FragmentManager fm;
	Fragment fragment[];
	String [] path;
	String count;
	mPagerAdapter adapter;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.notice_write_photo_view);
		toolbar = (Toolbar)findViewById(R.id.NOTICE_WRITE_PHOTO_TOOLBAR);
		mPager = (ViewPager)findViewById(R.id.NOTICE_WRITE_PHOTO_PAGER);
		fm = getSupportFragmentManager();
		count = getIntent().getStringExtra("count");
		path = getIntent().getStringArrayExtra("path");
		dataT = new ArrayList<CustomGallery>();
		for (String string : path) {
			CustomGallery item = new CustomGallery();
			item.sdcardPath = string;
			dataT.add(item);
		}
		fragment= new Fragment[dataT.size()];
		for (int i = 0; i < dataT.size(); i++) {
			fragment[i] = new Notice_Write_Photo_F(dataT.get(i).sdcardPath,i);
		}
		adapter = new mPagerAdapter(fm);
		mPager.setAdapter(adapter);
		mPager.setCurrentItem(Integer.parseInt(count));
		setStartToolbar();
		registerReceiver(mToastMessageReceiver, new IntentFilter(
				"com.defysoft.waukids.TOAST_MESSAGE"));
		
		
	}
	
	private void setStartToolbar() {
		toolbar.setNavigationOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	class mPagerAdapter extends FragmentPagerAdapter{

		public mPagerAdapter(FragmentManager fm) {
			super(fm);
			
		}
		
		@Override
		public Fragment getItem(int arg0) {
			return fragment[arg0];
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return dataT.size();
		}
		
	}
	
	
	class Notice_Write_Photo_F extends Fragment{
		TextView tv;
		GestureImageView gv;
		private ImageLoader imageLoader;
		DisplayImageOptions defaultOptions;
		String path_f,count;
		
		Notice_Write_Photo_F(String path,int count){
			path_f = path;
			this.count= (count+1)+"";
		}
		 
		@Override
		public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
				View v = inflater.inflate(R.layout.notice_write_photo_view_f, null);
				tv = (TextView)v.findViewById(R.id.NOTICE_WRITE_PHOTO_COUNT);
				gv = (GestureImageView)v.findViewById(R.id.NOTICE_WRITE_PHOTO_IV);
				Init();
				tv.setText(count+"/"+dataT.size());
				imageLoader.displayImage("file://" + path_f,gv,defaultOptions);
				tv.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						
						
					}
				});
			return v;
		}
		
		//이미지 로더 옵션
				void Init(){
					defaultOptions = new DisplayImageOptions.Builder()
					.cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
					.considerExifParams(true)					
					.bitmapConfig(Bitmap.Config.RGB_565).build();

					ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(
					getBaseContext())
					.defaultDisplayImageOptions(defaultOptions)
					.memoryCache(new WeakMemoryCache());

					ImageLoaderConfiguration config = builder.build();
					imageLoader = ImageLoader.getInstance();
					imageLoader.init(config);
				}
	}
	
	private final BroadcastReceiver mToastMessageReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.toast_layout, null);
			TextView toast_tv = (TextView)layout.findViewById(R.id.TOAST_LAYOUT_TV);
			Toast toast = new Toast(Notice_Write_Photo_View.this);
			toast.setView(layout);
			toast_tv.setText(intent.getStringExtra("PUSH_NAME")+" : "+intent.getStringExtra("PUSH_SUB"));
			toast.setDuration(Toast.LENGTH_LONG);
			toast.show();
		}
	};

	
}
